﻿package game.ui {
	import game.objects.GameObject;
	import game.debug.Debug;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Bitmap;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.display.MovieClip;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.filters.ColorMatrixFilter;
	
	public class Image extends GameObject {
		
		private var loader:Loader;
		private var container:MovieClip = new MovieClip;
		private var maska:MovieClip		= new MovieClip;
		private var text:Text;
		private var bitmapData:BitmapData;
		
		public function Image(data:*, width:Number, height:Number, ellipse:Number = 0):void {
			
			addChild(container);
			addChild(maska);
			
			text = new Text;
			text.setFormat("color", 0xCCCCCC);
			container.addChild(text);
			
			container.mask = maska;
			
			with (maska.graphics) {
				beginFill		(0xff0000, 1);
				drawRoundRect	(0, 0, width, height, ellipse, ellipse);
				endFill			();
			}
			
			
			loader = new Loader;
			loader.contentLoaderInfo.addEventListener ( ProgressEvent.PROGRESS,	progressHandler );
			loader.contentLoaderInfo.addEventListener ( Event.COMPLETE,			completeHandler );
			loader.contentLoaderInfo.addEventListener ( IOErrorEvent.IO_ERROR,	errorHandler );
			
			load(data);
			
		}
		
		public function load(data:*):void {
			if (data is BitmapData) {
				setBitmap( new Bitmap ( data as BitmapData ) );
			} else if (data is String) {
				try { loader.close(); } catch (e:Error) { }
				loader.unload();
				loader.load( new URLRequest ( data as String ), new LoaderContext(true) );
			}
		}
		
		private function setBitmap ( bitmap:Bitmap ):void {
			while (container.numChildren) container.removeChildAt(0);
			container.graphics.clear();
			
			if (bitmap.width>maska.width || bitmap.height>maska.height) {
				var crop:BitmapData = new BitmapData (maska.width, maska.height, true, 0x000000);
				var k:Number = Math.max (crop.width / bitmap.width, crop.height / bitmap.height);
				bitmap.width = bitmap.width * k;
				bitmap.height = bitmap.height * k;
				var m:Matrix = bitmap.transform.matrix;
				m.a = k;
				m.d = k;
				//var rect:Rectangle = new Rectangle((bitmap.width - bitmap.width*k)/2*k, 0, bitmap.width, bitmap.height);
				//rect пока не используется
				crop.draw ( bitmap, m, null, null, null, true );
				bitmap = new Bitmap ( crop, "auto", true );
			}
			
			bitmap.x = (maska.width - bitmap.width)/2;
			bitmap.y = (maska.height - bitmap.height)/2;
			
			if (!bitmapData)
				bitmapData = bitmap.bitmapData;
			
			container.addChild ( bitmap );
		}
		
		private function progressHandler ( event:ProgressEvent ):void {
			text.set( '%percent%%', {percent: String(Math.round(event.bytesLoaded/event.bytesTotal*100))}, false );
			text.x = (maska.width - text.width)/2;
			text.y = (maska.height - text.height)/2;
			with (container.graphics) {
				clear();
				beginFill(0x000000, 0.4);
				drawRect(0, 0, maska.width, maska.height*event.bytesLoaded/event.bytesTotal);
				endFill();
			}
		}
		
		private function completeHandler ( event:Event ):void {
			if (loader.content is Bitmap) {
				setBitmap (loader.content as Bitmap);
				
				this.dispatchEvent ( new Event ( Event.COMPLETE ) );
			} else
				Debug.log('Не является картинкой', Debug.INTERFACE);
				
				
		}
		
		private function errorHandler ( event:IOErrorEvent ):void {
			Debug.log('Картинка не загружена', Debug.INTERFACE);
			setBitmap ( new Bitmap ( new BitmapData (maska.width, maska.height, false, 0xff0000) ) );
		}
		
		public function blackAndWhite():void {
			
			if (!bitmapData) return;
			
			var db:BitmapData = bitmapData.clone();
			
			
			var arr:Array = [0.3, 0.6, 0.1, 0.0, 0.0,0.3, 0.6, 0.1, 0.0, 0.0,0.3, 0.6, 0.1, 0.0, 0.0,0.0, 0.0, 0.0, 1.0, 0.0];
			var cmf:ColorMatrixFilter = new ColorMatrixFilter(arr);
			db.applyFilter(db, new Rectangle(0, 0, db.width, db.height), new Point(0, 0), cmf);
			setBitmap( new Bitmap ( db ) );
			
		}
		
		public function color():void {
			if (!bitmapData) return;
			
			setBitmap( new Bitmap ( bitmapData ) );
			
		}
		
	}
	
}